package boss.portal.controller;


import boss.portal.common.CommonController;
import boss.portal.common.ResponseData;
import boss.portal.dao.PublicCollectCountDao;
import boss.portal.dto.BaseDelDto;
import boss.portal.dto.PublicCollectDto;
import boss.portal.dto.PublicSaveDto;
import boss.portal.entity.pro.PublicCollect;
import boss.portal.entity.pro.PublicCollectDetail;
import boss.portal.service.impl.pro.PublicCollectServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@Api(description = "公共采集", value = "公共采集")
@RestController
@RequestMapping("/public/collect")
public class PublicCollectController extends BaseController {

    @Autowired
    private PublicCollectServiceImpl publicCollectService;

    @PostMapping("/query")
    @ApiOperation("公共采集分页查询")
    public ResponseData<Page<PublicCollect>> queryPage(@RequestParam(value = "page",defaultValue = "0")int page,
                                  @RequestParam(value = "pageSize",defaultValue = "10")int pageSize,
                                  @RequestBody PublicCollectDto dto){
        Pageable pageable = PageRequest.of(page,pageSize, Sort.by(Sort.Direction.ASC, "createTime"));
        Page<PublicCollect> publicCollects = publicCollectService.queryPage(pageable, dto);
        return success(publicCollects);
    }

    @GetMapping("/query/detail/{detailId}")
    @ApiOperation(value = "详情查询,detailId 为详情ID")
    public ResponseData<PublicCollectDetail> queryDetail(@PathVariable("detailId") Long detailId){
        PublicCollectDetail detail = publicCollectService.querDetail(detailId);
        return ResponseData.success(detail);
    }

    @GetMapping("/header/count")
    @ApiOperation("公共采集头部统计")
    public ResponseData headerCountQuery(){
        PublicCollectCountDao publicCollectCountDao = publicCollectService.queryCount();
        return success(publicCollectCountDao);
    }

    @PostMapping("/add/remark/{id}")
    @ApiOperation("添加备注")
    public ResponseData updateRemark(@PathVariable("id")Long id,@RequestParam(value = "remark",required = true)String remark){
        publicCollectService.addRemark(id,remark);
        return success("添加备注成功！");
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation("删除")
    public ResponseData del(@PathVariable("id")Long id){
        publicCollectService.delete(id);
        return success("删除成功！");

    }

    @DeleteMapping("/delAll")
    @ApiOperation("批量删除")
    public ResponseData delAll(@RequestBody BaseDelDto baseDelDto){
        for (Long id:baseDelDto.getIds()){
            publicCollectService.delete(id);
        }
        return success("删除成功！");
    }

    @DeleteMapping("/del/choice")
    @ApiOperation("一键删除筛选结果全部产品")
    public ResponseData delChoice(@RequestBody PublicCollectDto dto){
        publicCollectService.delChoice(dto);
        return ResponseData.success();
    }

//    @PostMapping("/save/test")
//    @ApiOperation("新增数据sdf")
//    public ResponseData saveTestData(){
//        publicCollectService.addData();
//        return success("新增成功！");
//    }
    @PostMapping("/save")
    @ApiOperation("新增数据或者更新数据")
    public ResponseData saveOrUpData(@RequestBody PublicSaveDto publicSaveDto){
        try{
            publicCollectService.saveOrUpdateData(publicSaveDto);
        }catch (Exception e){
            logger.error("新增数据失败:"+e);
            throw new RuntimeException("新增数据失败！");
        }
        return success("新增数据成功！");
    }
}
